/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegistrationManager.java
 *
 * Created on Jun 15, 2009, 11:01:21 AM
 */
package mdnsapp;

import java.util.Hashtable;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import javax.swing.SwingUtilities;

/**
 *
 * @author Qaisar
 */
public class RegistrationManager extends javax.swing.JFrame {

    JmDNS dns;

    /** Creates new form RegistrationManager */
    public RegistrationManager() {

        try {
            dns = JmDNS.create();
        } catch (Exception ex) {
            System.err.println("Exception creating dns = " + ex.getMessage());
        }

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        RegPanel = new javax.swing.JPanel();
        ServiceName = new javax.swing.JTextField();
        ServicePort = new javax.swing.JTextField();
        ServiceWeight = new javax.swing.JTextField();
        ServicePriority = new javax.swing.JTextField();
        ServiceTxtKey = new javax.swing.JTextField();
        Register = new javax.swing.JButton();
        ServiceTypeLabel = new javax.swing.JLabel();
        ServiceNameLabel = new javax.swing.JLabel();
        ServicePortLabel = new javax.swing.JLabel();
        ServiceweightLabel = new javax.swing.JLabel();
        ServicePrioritylabel = new javax.swing.JLabel();
        ServiceTxtLabel = new javax.swing.JLabel();
        ServiceTxtValue = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        ServiceTypeCombo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(new java.awt.Rectangle(250, 200, 0, 0));
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mdnsapp.MDNSApp.class).getContext().getResourceMap(RegistrationManager.class);
        jLabel1.setText(resourceMap.getString("RegistrationManagerLabel.text")); // NOI18N
        jLabel1.setName("RegistrationManagerLabel"); // NOI18N

        RegPanel.setBackground(resourceMap.getColor("RegPanel.background")); // NOI18N
        RegPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        RegPanel.setName("RegPanel"); // NOI18N

        ServiceName.setText(resourceMap.getString("ServiceName.text")); // NOI18N
        ServiceName.setName("ServiceName"); // NOI18N

        ServicePort.setText(resourceMap.getString("ServicePort.text")); // NOI18N
        ServicePort.setName("ServicePort"); // NOI18N
        ServicePort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ServicePortActionPerformed(evt);
            }
        });

        ServiceWeight.setText(resourceMap.getString("ServiceWeight.text")); // NOI18N
        ServiceWeight.setName("ServiceWeight"); // NOI18N

        ServicePriority.setText(resourceMap.getString("ServicePriority.text")); // NOI18N
        ServicePriority.setName("ServicePriority"); // NOI18N

        ServiceTxtKey.setText(resourceMap.getString("ServiceTxtKey.text")); // NOI18N
        ServiceTxtKey.setName("ServiceTxtKey"); // NOI18N

        Register.setText(resourceMap.getString("Register.text")); // NOI18N
        Register.setName("Register"); // NOI18N
        Register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegisterActionPerformed(evt);
            }
        });

        ServiceTypeLabel.setText(resourceMap.getString("TypeLabel.text")); // NOI18N
        ServiceTypeLabel.setName("TypeLabel"); // NOI18N

        ServiceNameLabel.setText(resourceMap.getString("ServiceNameLabel.text")); // NOI18N
        ServiceNameLabel.setName("ServiceNameLabel"); // NOI18N

        ServicePortLabel.setText(resourceMap.getString("ServicePortLabel.text")); // NOI18N
        ServicePortLabel.setName("ServicePortLabel"); // NOI18N

        ServiceweightLabel.setText(resourceMap.getString("ServiceweightLabel.text")); // NOI18N
        ServiceweightLabel.setName("ServiceweightLabel"); // NOI18N

        ServicePrioritylabel.setText(resourceMap.getString("ServicePrioritylabel.text")); // NOI18N
        ServicePrioritylabel.setName("ServicePrioritylabel"); // NOI18N

        ServiceTxtLabel.setText(resourceMap.getString("ServiceTxtLabel.text")); // NOI18N
        ServiceTxtLabel.setName("ServiceTxtLabel"); // NOI18N

        ServiceTxtValue.setText(resourceMap.getString("ServiceTxtValue.text")); // NOI18N
        ServiceTxtValue.setName("ServiceTxtValue"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        ServiceTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "_bootstrapnode._tcp.local.", "_supernode._tcp.local.", "_clientnode._tcp.local.", "_http._tcp.local.", "_ftp._tcp.local.", "_tftp._tcp.local.", "_ssh._tcp.local.", "_smb._tcp.local.", "_printer._tcp.local.", "_airport._tcp.local.", "_afpovertcp._tcp.local.", "_ichat._tcp.local.", "_eppc._tcp.local.", "_presence._tcp.local." }));
        ServiceTypeCombo.setName("ServiceTypeCombo"); // NOI18N

        javax.swing.GroupLayout RegPanelLayout = new javax.swing.GroupLayout(RegPanel);
        RegPanel.setLayout(RegPanelLayout);
        RegPanelLayout.setHorizontalGroup(
            RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RegPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ServiceTypeLabel)
                    .addComponent(ServiceTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(RegPanelLayout.createSequentialGroup()
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addComponent(ServiceNameLabel)
                                .addGap(65, 65, 65))
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addComponent(ServiceName, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ServicePortLabel)
                            .addComponent(ServicePort, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addComponent(ServiceweightLabel))
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(ServiceWeight, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ServicePriority, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ServicePrioritylabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addComponent(ServiceTxtLabel)
                                .addGap(56, 56, 56))
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addComponent(ServiceTxtKey, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(ServiceTxtValue, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RegPanelLayout.createSequentialGroup()
                        .addComponent(Register)
                        .addContainerGap())))
        );
        RegPanelLayout.setVerticalGroup(
            RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RegPanelLayout.createSequentialGroup()
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(RegPanelLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ServiceTypeLabel)
                            .addComponent(ServiceNameLabel)
                            .addComponent(ServicePortLabel)
                            .addComponent(ServiceweightLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ServiceName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ServicePort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ServiceWeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ServiceTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29))
                    .addGroup(RegPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(ServiceTxtLabel)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(ServiceTxtKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(ServiceTxtValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(RegPanelLayout.createSequentialGroup()
                                .addComponent(ServicePrioritylabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ServicePriority, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(23, 23, 23))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RegPanelLayout.createSequentialGroup()
                .addContainerGap(80, Short.MAX_VALUE)
                .addComponent(Register))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(223, 223, 223)
                .addComponent(jLabel1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(RegPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addComponent(RegPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(170, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void RegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegisterActionPerformed
        // TODO add your handling code here:

        try {
           final String ST = (String)this.ServiceTypeCombo.getSelectedItem();
           final String serviceName = this.ServiceTxtValue.getText() + ":" +this.getServiceName()+"."+ST;
            final int port = Integer.parseInt(this.getServicePort());
            final int weight = Integer.parseInt(this.getServiceWeight());
            final int prio = Integer.parseInt(this.getServicePriority());
            //String txt = "path = " + this.getServiceTxt();
            final Hashtable txt = new Hashtable();
            txt.put(this.ServiceTxtKey.getText(), this.ServiceTxtValue.getText());

            SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            try{
                System.out.println("Goin to Register Service: Name = "+serviceName +" Type "+ST);
                dns.registerService(ServiceInfo.create(ST, serviceName, port, weight, prio, txt));
            }catch(Exception ex){System.err.println("Exception in RegistrationManagar.registerationpeformed = "+ex.getMessage());}
            }
        });
            
            
        } catch (Exception ex) {
            System.err.println("Exception registering service = " + ex.getMessage());
        }

}//GEN-LAST:event_RegisterActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    private void ServicePortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ServicePortActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ServicePortActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RegistrationManager().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel RegPanel;
    private javax.swing.JButton Register;
    private javax.swing.JTextField ServiceName;
    private javax.swing.JLabel ServiceNameLabel;
    private javax.swing.JTextField ServicePort;
    private javax.swing.JLabel ServicePortLabel;
    private javax.swing.JTextField ServicePriority;
    private javax.swing.JLabel ServicePrioritylabel;
    private javax.swing.JTextField ServiceTxtKey;
    private javax.swing.JLabel ServiceTxtLabel;
    private javax.swing.JTextField ServiceTxtValue;
    private javax.swing.JComboBox ServiceTypeCombo;
    private javax.swing.JLabel ServiceTypeLabel;
    private javax.swing.JTextField ServiceWeight;
    private javax.swing.JLabel ServiceweightLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

    public String getServiceName() {
        return ServiceName.getText();
    }

    public String getServicePort() {
        return ServicePort.getText();
    }

    public String getServicePriority() {
        return ServicePriority.getText();
    }

    public String getServiceTxt() {
        return ServiceTxtKey.getText();
    }
  public String getServiceWeight() {
        return ServiceWeight.getText();
    }
}
